package cn.tedu.boot61.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class SendController {
    @RequestMapping("/send")
    public String send(MultipartFile pic) throws IOException {
        System.out.println("pic = " + pic);
        String fileName = pic.getOriginalFilename();
        System.out.println("fileName = " + fileName);
        //获取文件类型
        String sufix = fileName.substring(fileName.lastIndexOf("."));
        //创建唯一文件名UUID
        fileName = UUID.randomUUID()+sufix;
        System.out.println( "new fileName = " + fileName);
        //准备保存图片的路径
        String dirPath = "D:/upload";
        File dirFile = new File(dirPath);
        if (!dirFile.exists()){
           dirFile.mkdirs(); 
        }
        //完整的文件路径
        String filePath = dirPath + "/" + fileName;
        System.out.println("filePath = " + filePath);
        //把文件保存到指定路径
        File file = new File(filePath);
        pic.transferTo(file);
        if (file.isFile()){
            System.out.println("yes");
            return fileName;
        }else {
            System.out.println("no");
            return "err";
        }

    }
}
